package com.hezhou.oauth2.web.entities;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import lombok.Data;
import org.apache.commons.collections.CollectionUtils;
import org.assertj.core.util.Lists;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.List;

/**
 * ---------------------------
 * (SysUser) 系统用户实体表
 * ---------------------------
 *
 * @Author: [hezhou]
 * @Date: 2020/3/6
 * @Version: [1.0.1]
 * ---------------------------
 */
@Data
public class SysUser implements UserDetails {

    @TableId(type = IdType.AUTO) // 表示主键自增长
    private Long id;

    private String username;
    /**
     * 权限集合 //他不是表中的数据 标识
     */
    @TableField(exist = false)
    private Collection<? extends GrantedAuthority> authorities;

    /**
     * 用户的角色列表
     */
    @TableField(exist = false)
    private List<SysRole> roleList = Lists.newArrayList();

    @TableField(exist = false)
    private List<Long> roleIds = Lists.newArrayList();

    public List<Long> getRoleIds() {
        if (CollectionUtils.isNotEmpty(roleList)){
            for (SysRole sysRole : roleList) {
                roleIds.add(sysRole.getId());
            }
        }
        return roleIds;
    }

    /**
     * 用户的权限列表
     */
    @TableField(exist = false)
    private List<SysPermission> sysPermissionList = Lists.newArrayList();


    /**
     * 密码需要通过加密后存储
     */
    private String password;
    /**
     * 帐户是否有效：1 未过期，0已过期
     * 因为是is 开头的boolean类型的  所以前端在接受值的时候要去掉is  isAccountNonExpired->AccountNonExpired
     * 1 true
     * 0 false
     */
    private boolean isAccountNonExpired = true;
    private boolean isAccountNonLocked = true;
    private boolean isCredentialsNonExpired = true;
    private boolean isEnabled = true;

    private String nickName;
    private String mobile;
    private String email;
    private String createDate;
    private String updateDate;

}
